package com.liting.uitest.imgutils;

import com.liting.uitest.model.GrayImagePixel;
import com.liting.uitest.model.ImagePixel;
import com.liting.uitest.model.LabColor;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import static org.opencv.core.CvType.CV_8U;

public class MinMaxPixel {
    static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

    public static double getColorDistance(Mat srcImage) {
        Core.MinMaxLocResult minMaxLocResult = getMinMaxPixel(srcImage);

        ImagePixel maxPixel = OpenCVImageUtils.getPixelByCoord(srcImage, (int) minMaxLocResult.maxLoc.x, (int) minMaxLocResult.maxLoc.y);
        ImagePixel minPixel = OpenCVImageUtils.getPixelByCoord(srcImage, (int) minMaxLocResult.minLoc.x, (int) minMaxLocResult.minLoc.y);

        LabColor maxLAB = RGBtoLAB.rgbToLab3(maxPixel.getRed(), maxPixel.getGreen(), maxPixel.getBlue());
        LabColor minLAB = RGBtoLAB.rgbToLab3(minPixel.getRed(), minPixel.getGreen(), minPixel.getBlue());

        double distance2 = ColorDistance.calculateColorDifference(maxLAB, minLAB);

        return distance2;
    }

    public static Core.MinMaxLocResult getMinMaxPixel(Mat srcImage) {
        // 读取图像并转为灰度图像
        //Mat srcImage = Imgcodecs.imread(imaFile);
        Mat grayImage = new Mat();
        if (srcImage.depth() != CV_8U) {
            Mat converted = new Mat();
            Core.convertScaleAbs(srcImage, converted, 1, 0);
            Imgproc.cvtColor(converted, grayImage, Imgproc.COLOR_BGR2GRAY);
        } else {
            Imgproc.cvtColor(srcImage, grayImage, Imgproc.COLOR_BGR2GRAY);
        }

        // 使用minMaxLoc查找灰度值最大和最小的像素及其坐标
        double[] minMax = new double[2];
        Point minLoc = new Point();
        Point maxLoc = new Point();
        //Core.minMaxLoc(grayImage, minMax, null, minLoc, maxLoc);
        Core.MinMaxLocResult minMaxLocResult = Core.minMaxLoc(grayImage);

        return minMaxLocResult;


    }

    public static void main(String[] args) throws Exception {
        Mat srcImage = Imgcodecs.imread("C:\\Users\\liting\\Pictures\\20240515000223049.jpg");
        Core.MinMaxLocResult minMaxLocResult = getMinMaxPixel(srcImage);

        ImagePixel maxPixel = OpenCVImageUtils.getPixelByCoord(srcImage, (int) minMaxLocResult.maxLoc.x, (int) minMaxLocResult.maxLoc.y);
        ImagePixel minPixel = OpenCVImageUtils.getPixelByCoord(srcImage, (int) minMaxLocResult.minLoc.x, (int) minMaxLocResult.minLoc.y);

        LabColor maxLAB = RGBtoLAB.rgbToLab3(maxPixel.getRed(), maxPixel.getGreen(), maxPixel.getBlue());
        LabColor minLAB = RGBtoLAB.rgbToLab3(minPixel.getRed(), minPixel.getGreen(), minPixel.getBlue());

        double distance = ColorDistance.calculateColorDifference(maxLAB, minLAB);

        double distance2 = ColorDistance.CIE_2000(maxLAB, minLAB);

        double distance3 = ColorDistance.calculateCIE2000(maxLAB, minLAB);


        // 输出灰度值最高的像素坐标
        System.out.println("图像中灰度最高的像素位置为：(" + minMaxLocResult.maxLoc.x + ", " + minMaxLocResult.maxLoc.y + ")，其灰度值为：" + minMaxLocResult.maxVal);
        System.out.println("图像中灰度最低的像素位置为：(" + minMaxLocResult.minLoc.x + ", " + minMaxLocResult.minLoc.y + ")，其灰度值为：" + minMaxLocResult.minVal);
        System.out.println("色差是" + distance);
        System.out.println("色差是" + distance2);
        System.out.println("色差是" + distance3);
    }
}
